Skip to main content
Version: 2.13.4

RS485, Modbus & NVT

api.rs485Send(msg)

Sends msg to RS485 bus.

Turn on RS485 using rs485State first.

api.rs485Setup(arg1, arg2, ...)

Change the configuration of RS485 interface.

api.rs485State(state)

Turns on the RS485 circuitry.

Must be used before rs485Send or rs485Receive.

api.rs485Receive(timeout,arg1,arg2)

Waits timeout milliseconds for data reception from RS485 bus.

Turn on RS485 using rs485State() function first.

After the first character is received the inter-character (i.e. inter-byte timeout) delay is 10ms. This can be changed by giving a integer number to arg1.

api.modbusCrc(msg)

Calculates Modbus request checksum.

api.modbusRTU(adress, registerAddr, functionCode, typeSpec, registerCount, timeout, retry, interByteTimeout, ignoreCRC)

The api.modbusRTU() function is used to communicate with a modbus device over a serial connection using the RTU (Remote Terminal Unit) protocol. It can be used to read or write one or more registers of various data types. The function takes several parameters, including the modbus device address, the starting register address, the function code, the type specification of the register(s), the number of registers to read or write, and several timeout and retry parameters.

The typeSpec parameter is a Lua string that specifies the data type of the register(s) being read or written. It uses the Lua Packing and Unpacking syntax to specify the type and formatting of the data. For example, to read a single 16-bit unsigned integer, the typeSpec parameter would be "H".

The function returns the value(s) read from or written to the modbus register(s), according to the specified typeSpec. For example, if typeSpec is "H" and registerCount is 2, the function would return two 16-bit unsigned integers.

If the function fails to communicate with the modbus device, it will retry the operation according to the retry parameter. If the operation still fails after the specified number of retries, the function will return nil.

api.nvtProcess(buf)

Processes NVT message and either sets baudrate, datasize, parity or stop size for MBUS or MODBUS.

api.nvtEncode(msg)

Encodes message to NVT format